<?php

/**
 * Created by PhpStorm.
 * User: feng
 * Date: 2016/12/17
 * Time: 上午11:28
 */
class Lyw_article_manager_model extends CI_Model
{

    //        id
//int(10) UN AI PK
//original_article_id
//int(11)
//translation_article_id
//int(11)
//proofread_article_id
//int(11)
//is_received 默认0未领取 1已领取 2已领取且翻译中
//int(1)
//is_translationed 默认0未翻译 1翻译中 2已经翻译
//int(1)
//is_proofreaded 默认0未校对 1校对中 2已校对
//int(1)
//creat_time
//int(10) UN
    public function __construct()
    {
        $this->load->database();
    }

    /**
     * 原文首次发表状态
     */
    public function originalStatus($original_article_id, $title)
    {
        $data = array('original_article_id' => $original_article_id, 'title' => $title, 'creat_time' => time());
        $str = $this->db->insert_string('lyw_article_manager', $data);
        $this->db->query($str);
        return $this->db->insert_id();
    }

    /**
     * @param $page 当前是第几页
     * @param $rows 每页多少项
     */
    public function getListInfo($page, $rows)
    {
        if ($page < 1) {
            $limit_begin = 0;
        } else {
            $limit_begin = ($page - 1) * $rows;
        }
        $limit_end = $rows;
//        $sql = "select * from lyw_article_manager where is_del = 0 order by id desc limit {$limit_begin} , {$limit_end} ";
        $sql = "select * from lyw_article_manager where is_del = 0 order by if(is_received <= 0 , 1 , 0) * 100000 + id desc limit {$limit_begin} , {$limit_end} ";
        $result = $this->db->query($sql)->result_array();
        return $result;
    }

    /**
     * 用户领取翻译任务
     */
    public function receivedStatus($user_id, $manager_id)
    {
        $this->db->trans_start();
        $this->load->model('lyw_translation_article_model');
        $insert_id = $this->lyw_translation_article_model->add($user_id);
        //添加翻译过期时间
        $data = array('is_received' => 1, 'translation_article_id' => $insert_id, 'update_time' => time(), 'translationed_time' => (time() + 60 * 60 * 24));
        $where = "id = {$manager_id} and is_received = 0 and translation_article_id = 0";
        $str = $this->db->update_string('lyw_article_manager', $data, $where);
        $this->db->query($str);
        $affected_num = $this->db->affected_rows();
        $this->db->trans_complete();
        if ($affected_num == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 取消领取状态
     */
    public function changeReceivedStatus($manager_id, $translation_id)
    {
        $this->db->trans_start();
        $this->load->model('lyw_translation_article_model');
        if ($this->lyw_translation_article_model->cancel($translation_id)) {
            $sql = 'update lyw_article_manager set translation_article_id = 0 , is_received = 0 ,is_translationed = 0, translationed_time =0,update_time = ? where id = ?';
            $this->db->query($sql, array(time(), $manager_id));
            $affected_rows = $this->db->affected_rows();
        }
        $this->db->trans_complete();
        if (isset($affected_rows) && $affected_rows == 1) {
            return true;
        } else {
            return false;
        }
    }

    public function countAll()
    {
        return $this->db->count_all('lyw_article_manager');
    }

    public function countIsNoDel()
    {
        $sql = 'select count(*) as count from lyw_article_manager where is_del = 0';
        $row = $this->db->query($sql)->row();
        return $row->count;
    }

    public function del($id)
    {
        $update_time = time();
        $sql = 'update lyw_article_manager set is_del = 1  , update_time = ? where id = ?';
        $this->db->query($sql, array($update_time, $id));
        if ($this->db->affected_rows()) {
            return true;
        } else {
            return false;
        }
    }

    public function getOneListInfoById($id)
    {
        $sql = 'select * from lyw_article_manager where id = ?';
        $result = $this->db->query($sql, array($id))->result_array();
        if (!empty($result)) {
            return $result[0];
        } else {
            return array();
        }
    }

    /**
     * @param $id_str
     * 任务专员获取自己的任务列表
     */
    public function getTaskWorkerOurselfTask($id_str, $page, $rows)
    {
        $limit_begin = ($page - 1) * $rows;
        $limit_end = $rows;
        $sql = "select * from lyw_article_manager where original_article_id in ({$id_str}) and is_del = 0 order by id desc limit {$limit_begin} , {$limit_end}";
        $result = $this->db->query($sql)->result_array();
        return $result;
    }

    /**
     * @param $id_str
     * 任务专员获取自己的任务列表数量
     */
    public function getTaskWorkerOurselfTaskCount($id_str)
    {
        $sql = "select count(*) as count from lyw_article_manager where original_article_id in ({$id_str}) and is_del = 0";
        $row = $this->db->query($sql)->row();
        return $row->count;
    }

    /**
     * @param $id_str
     * 译员获取自己的任务列表
     */
    public function getTranslationOurselfTask($id_str, $page, $rows)
    {
        $limit_begin = ($page - 1) * $rows;
        $limit_end = $rows;
        $sql = "select * from lyw_article_manager where translation_article_id in ({$id_str}) and is_del = 0 order by id desc limit {$limit_begin} , {$limit_end}";
        $result = $this->db->query($sql)->result_array();
        return $result;
    }

    /**
     * @param $id_str
     * 译员获取自己的任务列表数量
     */
    public function getTranslationOurselfTaskCount($id_str)
    {
        $sql = "select count(*) as count from lyw_article_manager where translation_article_id in ({$id_str}) and is_del = 0";
        $row = $this->db->query($sql)->row();
        return $row->count;
    }

    /**
     * 修改状态,原文状态使用originalStatus 这个方法添加数据,这里只负责修改
     */
    public function changeStatus($id, $column, $type)
    {
        $update_time = time();
        $sql = "update lyw_article_manager set {$column} = {$type} , update_time = {$update_time} where id = {$id}";
        $this->db->query($sql);
    }

    public function updateTitle($id, $tilte)
    {
        $sql = 'update lyw_article_manager set title = ? where id = ?';
        $this->db->query($sql, array($tilte, $id));
    }

    public function getFinishListInfo(&$manager_list)
    {
        $this->load->model('Lyw_original_article_model');
        $this->load->model('Lyw_translation_article_model');
        $this->load->model('Lyw_proofread_article_model');
        $this->load->model('Lyw_user_model');
        foreach ($manager_list as $k => &$v) {
            if ($v['is_translationed'] == 2) {
                $ta = $this->Lyw_translation_article_model->getOneListInfoById($v['translation_article_id']);
                $v['ta_update_time'] = date('Y-m-d H:i:s', $ta['update_time']);
            }
            if ($v['is_proofreaded'] == 2) {
                //寻找发表者
                $oa = $this->Lyw_original_article_model->getOneListInfoById($v['original_article_id']);
                $ta = $this->Lyw_translation_article_model->getOneListInfoById($v['translation_article_id']);
                $pa = $this->Lyw_proofread_article_model->getOneListInfoById($v['proofread_article_id']);
                $v['score'] = $pa['score'];
                $v['ta_update_time'] = date('Y-m-d H:i:s', $ta['update_time']);
                $oa_user = $this->Lyw_user_model->getUserInfor($oa['original_user_id']);
                $v['oa_name'] = $oa_user['name'];
                $v['oa_username'] = $oa_user['username'];
                $ta_user = $this->Lyw_user_model->getUserInfor($ta['translation_user_id']);
                $v['ta_name'] = $ta_user['name'];
                $v['ta_username'] = $ta_user['username'];
//                array_push($finish_arr, array($v['original_article_id'], $v['translation_article_id'], $v['proofread_article_id']));
            }
        }
    }

    public function updateReadNumber($manager_id, $read_number)
    {
        $sql = "update lyw_article_manager set read_number = ? where id = ?";
        $this->db->query($sql, array($read_number, $manager_id));
        if ($this->db->affected_rows() == 1) {
            return true;
        } else {
            return false;
        }
    }

    public function updateYungYinTitle($manager_id, $yungyin_title)
    {
        $sql = "update lyw_article_manager set yunying_title = ? where id = ?";
        $this->db->query($sql, array($yungyin_title, $manager_id));
        if ($this->db->affected_rows() == 1) {
            return true;
        } else {
            return false;
        }
    }

    public function unfinishedByUserId($user_id, $user_type)
    {
        if ($user_type == 1) {//译员
            $sql = "select a.id ,b.translation_user_id , a.title , a.is_translationed , a.translationed_time from lyw_article_manager as a LEFT JOIN lyw_translation_article as b on a.translation_article_id = b.id where b.translation_user_id = ? and a.is_translationed != 2 ORDER BY id DESC ;";
            $list = $this->db->query($sql, array($user_id))->result_array();
        } elseif ($user_type == 2) {
            $sql = "select b.original_user_id , a.title ,a.is_proofreaded from lyw_article_manager as a LEFT JOIN lyw_original_article as b on a.original_article_id = b.id where b.original_user_id = ? and a.is_proofreaded != 2";
            $list = $this->db->query($sql, array($user_id))->result_array();
        }
        return $list;
    }

    public function getTranslationOverDueList()
    {
        $sql = 'select * from lyw_article_manager where translationed_time < ? and translationed_time is not null and translationed_time != "" and translationed_time != 0 and is_translationed < 2';
        return $this->db->query($sql, array(time()))->result_array();
    }

    public function updateProofreadedNumber($manager_id, $proofreaded_number)
    {
        $sql = "update lyw_article_manager set proofreaded_number = ? , update_time = ? where id = ?";
        $this->db->query($sql, array($proofreaded_number, time(), $manager_id));
        return true;
    }

}